package de.wtschreiter.structured.document.io.event;

import java.util.*;

import de.wtschreiter.structured.document.io.event.basic.*;

public class EventBuilder {

    private Map<String, String> attributes;
    private byte[] binaryData;
    private CharSequence characterData;

    public EventBuilder appendCharacterData(final CharSequence characterData) {
        this.characterData = characterData;
        return this;
    }

    public EventBuilder appendBinaryData(final byte[] binaryData) {
        this.binaryData = binaryData;
        return this;
    }

    public EventBuilder appendAttribute(final String key, final String value) {
        getAttributes().put(key, value);
        return this;
    }

    public StartDocumentEvent createStartDocumentEvent() {
        final BasicStartDocument result = new BasicStartDocument();
        setAttributes(result);
        return result;
    }

    private void setAttributes(final AbstractBasicAttributedEvent event) {
        event.setAttributes(getAttributes());
    }

    private Map<String, String> getAttributes() {
        if (attributes == null) {
            attributes = new HashMap<>();
        }
        return attributes;
    }

    public EndDocumentEvent createEndDocumentEvent() {
        return new BasicEndDocumentEvent();
    }

    public StartElementEvent createStartElementEvent() {
        final BasicStartElementEvent result = new BasicStartElementEvent();
        setAttributes(result);
        return result;
    }

    public EndElementEvent createEndElementEvent() {
        return new BasicEndElementEvent();
    }

    public BinaryDataEvent createBinaryDataEvent() {
        final BasicBinaryDataEvent binaryDataEvent = new BasicBinaryDataEvent();
        binaryDataEvent.setData(binaryData);
        return binaryDataEvent;
    }

    public CharacterDataEvent createCharacterDataEvent() {
        final BasicCharacterDataEvent characterDataEvent = new BasicCharacterDataEvent();
        characterDataEvent.setData(characterData);
        return characterDataEvent;
    }

}
